package com.int_tech.facility.controller;

import com.int_tech.utils.GetUUID;
import com.int_tech.utils.Result;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

/**
 * Created by Administrator on 2017/10/16.
 */
@Controller("file1")
public class FileController1 {
    /**
     * 实现文件上传
     * */
    @RequestMapping("upload1")
    @ResponseBody
    public Result fileUpload(@RequestParam("fileName") MultipartFile file){
        if(file.isEmpty()){
            return Result.build(201,"路径为空");
        }
        String filename = file.getOriginalFilename();
        //取扩展名
        String extName = filename.substring(filename.lastIndexOf(".")+1);
        //使用uuid生成新的文件名
        String newriginalFilenameO = GetUUID.getId()+extName;

        String path = "F:/test" ;
        File dest = new File(path + "/" + newriginalFilenameO);
        try {
            file.transferTo(dest); //保存文件
            //返回图片路径
            String url = path+ "/"+newriginalFilenameO;
            return Result.ok(url);
        } catch (IllegalStateException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return Result.build(500,"上传失败");
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return Result.build(500,"上传失败");
        }
    }
}
